package com.luoye.lj.wstool.wsdm;

import com.luoye.lj.wstool.common.StringUtils;
import com.luoye.lj.wstool.generator.ICodeGenerator;
import com.luoye.lj.wstool.web.entity.WebtEntity;
import com.luoye.lj.wstool.web.entity.WebtField;
import com.luoye.lj.wstool.wsdm.entity.WsdtEntity;
import com.luoye.lj.wstool.wsdm.entity.WsdtField;
import com.luoye.lj.wstool.wsdm.entity.WsdtFieldDesc;

/*
 * Created with IntelliJ IDEA.
 * User: Dave Liu
 * Date: 2016/7/7
 * Time: 15:45
 * To change this template use File | Settings | File Templates.
 */
public class HtmlSearchTargetCodeGenerator implements ICodeGenerator {
    private WsdtEntity wsdtEntity;

    public HtmlSearchTargetCodeGenerator(WsdtEntity wsdtEntity) {
        this.wsdtEntity = wsdtEntity;
    }

    @Override
    public String codeGenerate() {
        String code="";
        int i=0;
        if(this.wsdtEntity!=null){
            if(this.wsdtEntity.getFieldList()!=null){
                for(WsdtField wsdtField:this.wsdtEntity.getFieldList()){
                    String type="number";
                    if(wsdtField.getFieldType().equals("Date")){
                        type="datetime-local";
                    }else if(wsdtField.getFieldType().equals("String")){
                        type="text";
                    }
                    if(wsdtField.getWsdtFieldDesc()==null) {
                        if (i % 4 == 0) {
                            code += "            <div class=\"row screen-group\">\n";
                        }
                        code += "" +
                                "                <div class=\"col-lg-3 col-md-3 col-sm-3 col-xs-12 form-group\">\n" +
                                "                        <label>" + wsdtField.getDescription() + "：</label>\n" +
                                "                        <input class=\"form-control\" type=\""+type+"\" id=\"" + wsdtField.getFieldName() + "\"/>\n" +
                                "                </div>\n";
                        if (i % 4 == 3) {
                            code += "            </div>\n";
                        }
                        i++;
                    }else{
                        if(wsdtField.getWsdtFieldDesc().isSearch()){
                            if (i % 4 == 0) {
                                code += "            <div class=\"row screen-group\">\n";
                            }
                            code += "" +
                                    "                <div class=\"col-lg-3 col-md-3 col-sm-3 col-xs-12 form-group\">\n" +
                                    "                        <label>" + wsdtField.getWsdtFieldDesc().getText() + "：</label>\n";
                            if(wsdtField.getWsdtFieldDesc().getEnumItems()!=null){
                                code += "                        <select class=\"form-control\" type=\"text\" id=\"" + wsdtField.getFieldName() + "\">\n" +
                                        "                        <option value=\"\">全部</option>\n";
                                for(WsdtFieldDesc.EnumItem enumItem:wsdtField.getWsdtFieldDesc().getEnumItems()){
                                   code+="                        <option value=\""+enumItem.getValue()+"\">"+enumItem.getText()+"</option>\n";
                                }
                                code += "                        </select>\n";
                            }else if(wsdtField.getWsdtFieldDesc().getRelatedEntity()!=null){
                                code += "                        <select class=\"form-control\" type=\"text\" id=\"" + wsdtField.getFieldName() + "\">\n" +
                                        "                        <option value=\"\">全部</option>\n";
                                String entityName=wsdtField.getWsdtFieldDesc().getRelatedEntity().getEntityName();
                                String en= StringUtils.makeFirstCharLowercase(entityName);
                                String v=StringUtils.makeFirstCharLowercase(wsdtField.getWsdtFieldDesc().getRelatedEntity().getValueField());
                                String t=StringUtils.makeFirstCharLowercase(wsdtField.getWsdtFieldDesc().getRelatedEntity().getTextField());
                                code += "                        <option th:each=\""+en+":\\${"+en+"List}\" th:value=\"\\${"+en+"."+v+"}\" th:text=\"\\${"+en+"."+t+"}\"></option>\n";
                                code += "                        </select>\n";
                            }else{
                                code += "                        <input class=\"form-control\" type=\""+type+"\" id=\"" + wsdtField.getFieldName() + "\"/>\n";
                            }
                            code += "                </div>\n";
                            if (i % 4 == 3) {
                                code += "            </div>\n";
                            }
                            i++;
                        }
                    }
                }
            }
        }
        if(i%4!=0){
            code+="            </div>\n";
        }
        return code;
    }
}
